const mongoose = require("mongoose")
const carsModel = mongoose.model("cars")

module.exports.add = async data => {
  const result = await carsModel.create(data)
  return result
}
module.exports.list = async data => {
  const result = await carsModel.find()
  return result
}
module.exports.change = async data => {
  const result = await carsModel.updateOne({_id:data.id},{})
  return result
}
module.exports.ban = async id => {
  console.log(id)
  state = await carsModel.findOne({_id: id})
  console.log(state)
  const result = await carsModel.updateOne({_id: id}, { rentState: !state.rentState,})
  console.log(result)
  return result
}
module.exports.comList = async id => {

  const result = await carsModel.find({})
  console.log(result)
  var base64img = function(file){
  let filePath = path.resolve(file);
  let data = fs.readFileSync( path.resolve(filePath));
  data = new Buffer(data).toString('base64');
  return 'data:' + mineType.lookup(filePath) + ';base64,' + data;
}
  return result
}

module.exports.applyCheckPass = async data => {

  let result
  if(data.pass) {
    result = await carsModel.updateOne({_id: data.id}, {check: false, rentState: true,passState:true})
  }else {
    result = await carsModel.updateOne({_id: data.id}, {check: false})
  }

  return result
}
module.exports.getCarsById = async id => {
  const result = await carsModel.findOne({_id:id})
  return result
}

